<?php 
/**
* 上传类
*/
class Upload
{
	public $config = array(
		'upload_dir'=>'./Upload',
		'max_size'=>4000000,
		'suffiux'=>['jpg','png','gif']
		);
	public $data;
	public $errorInfo;
	public $suffix;
	public function __construct($configs=array())
	{
		$this->config=array_merge($this->config,$configs);
	}

	public function getInfo()
	{
		return $this->errorInfo;
	}

	public function Upload()
	{
		$this->data = current($_FILES);

		if(!$this->checkError()) return false; 

		if(!$this->checkSize()) return false;

		if(!$this->checkType()) return false;

		if(!$this->moveFile()) return false;

		return true;

	}

	protected function checkError ()
	{
		if($this->data['error']>0){
			$this->getError($this->data['error']);
			return false;
		}

		return true;
	}

	protected function getError($num)
	{
		switch ($num) {
			case 1:
				$this->errorInfo = "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值:错误代码：".$num;
				break;
			case 2:
				$this->errorInfo = "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的:错误代码：".$num;
				break;
			case 3:
				$this->errorInfo = "文件只有部分被上传:错误代码：".$num;
				break;
			case 4:
				$this->errorInfo = "系统未知错误：错误代码:".$num;
				break;
		}
	}

	protected function checkSize ()
	{
		if($this->data['size']>$this->config['max_size']){
			$this->errorInfo ="大小超过限制,限制大小为：".$this->config['max_size'];
			return false;
		}
		return true;
	}

	protected function checkType ()
	{
		$arr = explode('.', $this->data['name']);

		$this->suffix = end($arr);

		if(!in_array($this->suffix, $this->config['suffiux'])){

			$this->errorInfo ="文件上传的类型不符合,要求的上传类型是：".implode(',', $this->config['suffiux']);

			return false;

		}
		return true;
	}

	protected function moveFile ()
	{
		is_dir($this->config['upload_dir']) || mkdir($this->config['upload_dir']);

		$path = $this->config['upload_dir'].'/'.date("Y-m-d");

		is_dir($path) || mkdir($path);

		$name = uniqid();

		$res = move_uploaded_file($this->data['tmp_name'],$path.'/'.$name.$this->suffix);

		if($res) return true;

	}
}









 ?>